<!-- 配置车辆弹窗 -->
<template>
  <div>
    <el-dialog
      title="配置车辆"
      :visible="visible"
      width="40%"
      :close-on-click-modal="false"
      @close="$emit('update:visible', false)"
    >
      <div class="header">
        <div>司机编号：{{ carDetail.userId }}</div>
        <div>司机名称：{{ carDetail.name }}</div>
        <div>司机电话：{{ carDetail.mobile }}</div>
        <div>所属机构：{{ carDetail.agency.name }}</div>
      </div>
      <!-- 表单 -->
      <el-form
        label-width="80px"
      >
        <el-form-item label="配置车辆:">
          <el-select
            v-model="form.licensePlate"
            placeholder="请选择配置车辆"
            style="width: 240px;"
            @change="changeSelect"
          >
            <el-option
              v-for="item in optionList"
              :key="item.id"
              :label="item.licensePlate"
              :value="item.licensePlate"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column
          prop="licensePlate"
          label="车牌号"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="workStatus"
          label="车辆状态"
          width="180"
        >
          <template #default="{row}">
            <span
              class="tableColumn-status"
              :class="row.workStatus? '':'stop-use'"
            >
              {{ row.workStatus?'可用':'停用' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
        >
          <template #default="{row}">
            <el-button
              type="text"
              size="small"
              @click="$router.push({
                name:'vehicle-detail',
                query:{
                  id: row.id,
                  formUrlName:'车辆管理'
                }
              })"
            >查看</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button
              type="text"
              size="small"
              style="color: #f56c6c;"
              @click="del"
            >删除</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <img
            class="icon"
            src="@/assets/empty.png"
            style="width: 100px;margin-top: 20px;"
          />
          <div style="margin: -20px 0 10px;">这里空空如也</div>
        </div>
      </el-table>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="warning"
          @click="changeCarConfig"
        >确 定</el-button>
        <el-button
          plain
          style="width: 100px;"
          @click="$emit('update:visible',false)"
        >取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { driverArrangeCar } from '@/api/transit'
export default {
  name: 'CarConfig',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    carDetail: {
      type: Object,
      default: () => ({})
    },
    truckList: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      form: {}, // 表单数据
      optionList: [], // 下拉框列表
      tableData: [] // 表格数据
    }
  },
  created() {
    if (this.carDetail.truck) {
      this.form.licensePlate = this.carDetail.truck.licensePlate
      this.tableData = [this.carDetail.truck]
      // 下拉列表只展示此司机擅长的车辆
      this.optionList = this.truckList.filter(item => item.truckTypeId === this.carDetail.truck.truckTypeId)
    } else {
      this.optionList = this.truckList
      this.tableData = []
    }
  },
  methods: {
    // 下拉框中选择车辆
    changeSelect() {
      this.tableData = [this.optionList.find(item => item.licensePlate === this.form.licensePlate)]
    },
    // 删除配置车辆
    del() {
      this.form = {}
      this.tableData = []
    },
    // 提交---修改配置车辆
    async changeCarConfig() {
      if (this.tableData.length === 0) {
        this.$message.error('配置失败，请先完善车辆信息')
        return
      }
      const res = await driverArrangeCar({
        driverIds: [this.carDetail.userId],
        truckId: this.tableData[0].id
      })
      if (res.msg === 'ok') {
        this.$message.success('安排车辆成功')
        this.$emit('updateList')
        this.$emit('update:visible', false)
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.serchBox {
  width: 584px;
  margin: 0 auto;
  margin-top: 19px;
  .el-select {
    width: 238px;
  }
  margin-bottom: 20px;
}
.workTime {
  white-space: nowrap;
}
.header {
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
  width: 574px;
  height: 90px;
  background-color: #fbfafa;
  flex-wrap: wrap;
  padding: 21px 52px;
  margin: 0 auto;
  // 使得整体内容距离上部分的距离为20px
  margin-top: -10px;
  .el-input {
    width: 240px;
  }
  div {
    flex: 40%;
     // 使包裹的所有div的上下间距为14px
    margin-bottom: 14px;
  }
  .num,
  .iphone{
    flex: 60%;
  }
  .num,.name{
    margin-bottom: 14px;
  }
  label {
    width: 71px;
    height: 20px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    font-size: 14px;
    color: #20232A;
    letter-spacing: 0.16px;
    text-align: right;
  }
  span {
    color: #818693;
    font-size: 14px;
  }
}
.pagination {
  margin: 32px 0 44px !important;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px !important;
  color: #20232a !important;
}
.selected-driver {
  margin-top: 18px;
}

/deep/ .el-dialog__footer{
  text-align: center;
}
.el-form{
  margin: 20px 0;
  /deep/ .el-form-item__label{
    color: #606266;
  }
}
</style>
